//
//  VisitorLoginView.swift
//  HMWeibo08
//
//  Created by 唐万超 on 15/7/29.
//  Copyright © 2015年 唐万超. All rights reserved.
//

import UIKit

// MARK: - 手写代理
protocol VisitorLoginViewDelegate: NSObjectProtocol {

    // 代理方法
    func visitorViewWillRegister()
    
    func visitorViewWillLogin()
    
}


class VisitorLoginView: UIView {

    // 定义代理属性
    weak var delegate: VisitorLoginViewDelegate?
    
    // MARK: - 设置视图信息
    /// 设置访客视图信息
    func setUpVisitorView(isHome:Bool, iconViewName:String, message:String) {
    
        iconView.image = UIImage(named: iconViewName)
        
        infoLabel.text = message
        
        homeView.hidden = !isHome
        
        // 判断是不是主页，不是就把遮罩放到后面
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
        
    }
    
    private func startAnimation() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 20
        
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    // MARK: - 监听按钮的点击
    func registerBtnClick() {
    
        //print("注册")
        delegate?.visitorViewWillRegister()
        
    }
    
    func loginBtnClick() {
        
        //print("登录")
        delegate?.visitorViewWillLogin()
        
    }
   
    // MARK: - 设置界面
    // 创建实例化view
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
        
    }

    // 当从sb或者xib创建时调用
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
        setUpUI()
    }
    
    /// 设置界面元素
   private func setUpUI() {
        
        self.backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        // 添加UI子控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeView)
        addSubview(infoLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
    
        /// 设置子控件的约束
        // 图标的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        
        // 遮罩的约束    VFL 可视化格式语言
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subView]-(-35)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView":maskIconView, "regButton": registerButton]))
        
        
        // 首页小房子的约束
        homeView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        // 描述文字的约束
        infoLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: infoLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: infoLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        // 设置文字的宽度约束
        addConstraint(NSLayoutConstraint(item: infoLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        // 设置注册按钮约束
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        // 左
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: infoLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        // 上下
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: infoLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        // 宽
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        // 高
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
        // 设置登录按钮约束
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        // 左
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: infoLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        // 上下
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: infoLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        // 宽
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        // 高
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
    }
    
    
    // MARK: - 懒加载子控件
      // 最简单的懒加载
      // private lazy var iconView = UIImageView()
    /// 图标
    private lazy var iconView: UIImageView = {
    
        let iconImg = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
        
        return iconImg
        
    }()
    
    /// 遮罩的懒加载
    private lazy var maskIconView: UIImageView = {
    
        let miv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return miv
        
    }()
    
    /// 首页小房子图标
    private lazy var homeView: UIImageView = {
        
        let homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return homeView
        
    }()
    
    /// 文字描述
    private lazy var infoLabel: UILabel = {
    
        let infoLabel = UILabel()
        
        infoLabel.text = "关注一些人，回这里看看有什么惊喜"
        
        infoLabel.sizeToFit()
        
        // 设置文字的大小和颜色
        infoLabel.textColor = UIColor.darkGrayColor()
        
        infoLabel.font = UIFont.systemFontOfSize(14)
        
        // 设置文字换行
        infoLabel.numberOfLines = 0
        
        // 设置文字居中显示
        infoLabel.textAlignment = NSTextAlignment.Center
        
        return infoLabel
        
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
    
        // 这种创建的是自定义button
        let registerButton = UIButton()
        
        // 设置按钮的文字和背景图片
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        registerButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        registerButton.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerButton
        
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
    
        let loginButton = UIButton()
        
        // 设置按钮的文字和背景图片
        loginButton.setTitle("登录", forState: UIControlState.Normal)
        
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        loginButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        loginButton.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginButton
        
    }()
    
    

}
